package com.dz51.core.page.taglib;

import java.io.Writer;
import java.lang.reflect.Method;
import java.text.MessageFormat;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.dz51.core.utils.PropertyUtil;

@SuppressWarnings("unused")
public class PageTag extends SimpleTagSupport {
	protected String DEFAULT_TABLE_WIDTH = "90%";
	protected String DEFAULT_TABLE_CLASS = "tablepagenum";
	private static final String DEFAULT_RETURN = "\r\n";
	private String commondName;
	private String classType;
	private String tableWidth;
	private String btnClass;
	private String gotoInputClass;
	private String tdClass;
	private String composition;
	private String hasPaggingToolbar;
	private String paggingButtonStyle;
	private String goToButtonClass;
	private String goToTdClass;
	private static final String OUT_HEAD_BEGIN = "<TABLE class=\"{0}\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\"> \r\n";
	private static final String ALREADY_HAS_TOOLBAR_YES = " <TR> \r\n";
	private static final String ALREADY_HAS_TOOLBAR_NO = "<input type=\"hidden\" name=\"page\" value=\"{1}\"/> \r\n <TR> \r\n";
	private static final String OUT_HEAD_END = "\r\n </TR> \r\n </TABLE> ";
	private static final String PAGE_INFO_TD = "<TD class=\"{9}\"><span>共{2}页</span><span>第{1}页</span><span>共{4}条数据</TD> ";
	private static final String PAGE_OPERATE_LABEL_TD = "<TD class=\"{9}\"><a class=\"{7}\" onClick=\"pageHandle(1)\" style=\"cursor:pointer;\">首页</a><a class=\"{7}\" onClick=\"pageHandle({5})\" style=\"cursor:pointer;\">上一页</a><a class=\"{7}\" onClick=\"pageHandle({6})\" style=\"cursor:pointer;\">下一页</a><a class=\"{7}\" onClick=\"pageHandle({2})\" style=\"cursor:pointer;\">末页</a></TD> \r\n";
	private static final String PAGE_OPERATE_BUTTON_TD = "<TD class=\"{9}\"><input type=\"button\" class=\"{7}\" value=\"首页\" onClick=\"pageHandle(1)\"/><input type=\"button\" class=\"{7}\" value=\"上一页\" onClick=\"pageHandle({5})\"/><input type=\"button\" class=\"{7}\"  value=\"下一页\" onClick=\"pageHandle({6})\"/><input type=\"button\" class=\"{7}\" value=\"末页\" onClick=\"pageHandle({2})\"/></TD> \r\n";
	private static final String PAGE_GOTO_OPERATE_TD = "<TD class=\"{11}\"><span>第</span><input type=\"text\" id=\"{3}\" size=\"4\" name=\"goTo\" value=\"{1}\" class=\"{8}\"/><span>页</span><input type=\"button\" class=\"{10}\"  value=\"跳转\" onclick=\"pageHandleGoTo(''{3}'');\"/></TD>";

	public String getGoToTdClass() {
		if (this.goToTdClass == null) {
			this.goToTdClass = this.tdClass;
		}
		return this.goToTdClass == null ? "" : this.goToTdClass;
	}

	public void setGoToTdClass(String goToTdClass) {
		this.goToTdClass = goToTdClass;
	}

	public String getGoToButtonClass() {
		return this.goToButtonClass;
	}

	public void setGoToButtonClass(String goToButtonClass) {
		this.goToButtonClass = goToButtonClass;
	}

	public String getPaggingButtonStyle() {
		return this.paggingButtonStyle;
	}

	public void setPaggingButtonStyle(String paggingButtonStyle) {
		this.paggingButtonStyle = paggingButtonStyle;
	}

	public String getComposition() {
		return this.composition;
	}

	public void setComposition(String composition) {
		this.composition = composition;
	}

	public String getHasPaggingToolbar() {
		return this.hasPaggingToolbar;
	}

	public void setHasPaggingToolbar(String hasPaggingToolbar) {
		this.hasPaggingToolbar = hasPaggingToolbar;
	}

	public void doTag() {
		try {
			Object commondObject = getCommondObject();

			Integer page = Integer.valueOf(Integer.parseInt(getCommondObjectValue(commondObject, "getPage")));
			Integer maxPage = Integer.valueOf(Integer.parseInt(getCommondObjectValue(commondObject, "getMaxPage")));
			Long count = Long.valueOf(Long.parseLong(getCommondObjectValue(commondObject, "getCount")));

			String pageToolbar = "<TABLE class=\"{0}\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\"> \r\n";
			if ((this.hasPaggingToolbar != null) || (Boolean.valueOf(this.hasPaggingToolbar).booleanValue())) {
				pageToolbar = pageToolbar + " <TR> \r\n";
			} else {
				pageToolbar = pageToolbar + "<input type=\"hidden\" name=\"page\" value=\"{1}\"/> \r\n <TR> \r\n";
			}
			if ((this.composition == null) || ("".equals(this.composition))) {
				pageToolbar = pageToolbar
						+ "<TD class=\"{9}\"><span>共{2}页</span><span>第{1}页</span><span>共{4}条数据</TD> "
						+ getPageOperateHTML()
						+ "<TD class=\"{11}\"><span>第</span><input type=\"text\" id=\"{3}\" size=\"4\" name=\"goTo\" value=\"{1}\" class=\"{8}\"/><span>页</span><input type=\"button\" class=\"{10}\"  value=\"跳转\" onclick=\"pageHandleGoTo(''{3}'');\"/></TD>";
			} else {
				String[] split = this.composition.split("[ ,;]");
				for (int i = 0; i < split.length; i++) {
					if (split[i].equalsIgnoreCase("PAGE_INFO")) {
						pageToolbar = pageToolbar + "<TD class=\"{9}\"><span>共{2}页</span><span>第{1}页</span><span>共{4}条数据</TD> ";
					} else if (split[i].equalsIgnoreCase("PAGE_OPERATE")) {
						pageToolbar = pageToolbar + getPageOperateHTML();
					} else if (split[i].equalsIgnoreCase("PAGE_GOTO_OPERATE")) {
						pageToolbar = pageToolbar
								+ "<TD class=\"{11}\"><span>第</span><input type=\"text\" id=\"{3}\" size=\"4\" name=\"goTo\" value=\"{1}\" class=\"{8}\"/><span>页</span><input type=\"button\" class=\"{10}\"  value=\"跳转\" onclick=\"pageHandleGoTo(''{3}'');\"/></TD>";
					}
				}
			}
			pageToolbar = pageToolbar + "\r\n </TR> \r\n </TABLE> ";

			String output = MessageFormat.format(
					pageToolbar,
					new Object[] { getClassType(), page, maxPage, "id" + (int) (Math.random() * 10000000.0D + 1.0D) + "1", count,
							Integer.valueOf(page.intValue() - 1), Integer.valueOf(page.intValue() + 1), this.btnClass, this.gotoInputClass,
							this.tdClass, this.goToButtonClass, this.goToTdClass });

			Writer out = super.getJspContext().getOut();
			out.write(output);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getPageOperateHTML() {
		if ((this.paggingButtonStyle == null) || ("LABEL".equals(this.paggingButtonStyle.trim()))) {
			return "<TD class=\"{9}\"><a class=\"{7}\" onClick=\"pageHandle(1)\" style=\"cursor:pointer;\">首页</a><a class=\"{7}\" onClick=\"pageHandle({5})\" style=\"cursor:pointer;\">上一页</a><a class=\"{7}\" onClick=\"pageHandle({6})\" style=\"cursor:pointer;\">下一页</a><a class=\"{7}\" onClick=\"pageHandle({2})\" style=\"cursor:pointer;\">末页</a></TD> \r\n";
		}
		return "<TD class=\"{9}\"><input type=\"button\" class=\"{7}\" value=\"首页\" onClick=\"pageHandle(1)\"/><input type=\"button\" class=\"{7}\" value=\"上一页\" onClick=\"pageHandle({5})\"/><input type=\"button\" class=\"{7}\"  value=\"下一页\" onClick=\"pageHandle({6})\"/><input type=\"button\" class=\"{7}\" value=\"末页\" onClick=\"pageHandle({2})\"/></TD> \r\n";
	}

	public String getTablewidth() {
		if (this.tableWidth == null) {
			return this.DEFAULT_TABLE_WIDTH;
		}
		return this.tableWidth;
	}

	private Object getCommondObject() throws Exception {
		Object commondObject = null;
		String commondName = getCommondName();
		if (PropertyUtil.objectNotEmpty(commondName)) {
			JspContext jspContext = super.getJspContext();
			commondObject = jspContext.getAttribute(commondName, jspContext.getAttributesScope(commondName));
		} else {
			System.err.println("commondName为空");
		}
		return commondObject;
	}

	private String getCommondObjectValue(Object commondObject, String commondMethod) throws Exception {
		String value = null;
		Method method = commondObject.getClass().getMethod(commondMethod, new Class[0]);
		Object result = method.invoke(commondObject, new Object[0]);
		if (result != null) {
			value = result.toString();
		} else {
			System.err.println(commondMethod + "()方法返回值为空");
		}
		return value;
	}

	public String getCommondName() {
		return this.commondName;
	}

	public void setCommondName(String commondName) {
		this.commondName = commondName;
	}

	public String getClassType() {
		if (this.classType == null) {
			return this.DEFAULT_TABLE_CLASS;
		}
		return this.classType;
	}

	public void setClassType(String classType) {
		this.classType = classType;
	}

	public String getTableWidth() {
		return this.tableWidth;
	}

	public void setTableWidth(String tableWidth) {
		this.tableWidth = tableWidth;
	}

	public String getBtnClass() {
		return this.btnClass == null ? "" : this.btnClass;
	}

	public void setBtnClass(String btnClass) {
		this.btnClass = btnClass;
	}

	public String getGotoInputClass() {
		return this.gotoInputClass;
	}

	public void setGotoInputClass(String gotoInputClass) {
		this.gotoInputClass = gotoInputClass;
	}

	public String getTdClass() {
		return this.tdClass;
	}

	public void setTdClass(String tdClass) {
		this.tdClass = tdClass;
	}
}
